package app.modules.cloudpivot.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.view.ResponseResult;

/**
 * 自定义Controller
 *
 * @author LiuGangQiang Create in 2024/08/08
 */
@RestController
@RequestMapping("/extend/test")
public class TestController extends BaseController {
    private final static Logger LOG = LoggerFactory.getLogger(TestController.class);

    private static final String CHINESE_CHARS = "一二三四五六七八九十春夏秋冬风花雪月山海天地日月星辰光影尘音时梦心意流云江湖故人长安少年剑酒琴书诗画的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进着等部度家电力里如水化高自二理起小物现实加量都两体制机当使点从业本去把性好应开它合还因由其些然前外天政四日那社义事平形相全表间样与关各新线内数正心反你明看原又么利比或但质气第向道命此变条只没结解问意建月公无系军很情者最立代想已通并提直题党程展五果料象员革位入常文总次品式活设及管特件长求老头基资边流路级少图山统接知较将组见计别她手角期根论运农指几九区强放决西被干做必战先回则任取据处队南给色光门即保治北造百规热领七海口东导器压志世金增争济阶油思术极交受联什认六共权收证改清已或见办史八王完类才政研务族式难备青拉洲校造元名社友诉军策功约收断托帮景习跳限府车铁装率深務标专干断准危简华盛顿科技梦想成功家庭幸福自由和平";

    private String getRandomChineseString(int length) {
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHINESE_CHARS.length());
            sb.append(CHINESE_CHARS.charAt(index));
        }
        return sb.toString();
    }

    private int getRandomNumber(int min, int max) {
        return ThreadLocalRandom.current().nextInt(min, max + 1);
    }

    private Date getRandomDate() {
        long now = System.currentTimeMillis();
        long randomTime = now - ThreadLocalRandom.current().nextLong(1L * 365 * 24 * 60 * 60 * 1000);
        return new Date(randomTime);
    }

    @PostMapping("/datas")
    public ResponseResult<?> datas(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        LOG.info("===>>模拟第三方接口，随机生成数据");

        // 基础数据类型
        result.put("stringValue", getRandomChineseString(6));
        result.put("currentDate", getRandomDate());
        result.put("numericValue", getRandomNumber(10, 300));

        // 嵌套对象
        Map<String, Object> nestedObject = new HashMap<>();
        nestedObject.put("nestedString", getRandomChineseString(5));
        nestedObject.put("nestedDate", getRandomDate());
        nestedObject.put("nestedNumber", getRandomNumber(10, 300));
        result.put("nestedData", nestedObject);

        // 对象列表
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 第一个数据项
        Map<String, Object> firstItem = new HashMap<>();
        firstItem.put("itemName", getRandomChineseString(4));
        firstItem.put("itemDate", getRandomDate());
        firstItem.put("itemScore", getRandomNumber(10, 300));

        List<Map<String, Object>> items1 = new ArrayList<>();
        Map<String, Object> item1_1 = new HashMap<>();
        item1_1.put("subName", getRandomChineseString(3));
        item1_1.put("subValue", getRandomNumber(1, 100));
        Map<String, Object> item1_2 = new HashMap<>();
        item1_2.put("subName", getRandomChineseString(3));
        item1_2.put("subValue", getRandomNumber(1, 100));
        items1.add(item1_1);
        items1.add(item1_2);
        firstItem.put("items", items1);

        // 第二个数据项
        Map<String, Object> secondItem = new HashMap<>();
        secondItem.put("itemName", getRandomChineseString(4));
        secondItem.put("itemDate", getRandomDate());
        secondItem.put("itemScore", getRandomNumber(10, 300));

        List<Map<String, Object>> items2 = new ArrayList<>();
        Map<String, Object> item2_1 = new HashMap<>();
        item2_1.put("subName", getRandomChineseString(3));
        item2_1.put("subValue", getRandomNumber(1, 100));
        Map<String, Object> item2_2 = new HashMap<>();
        item2_2.put("subName", getRandomChineseString(3));
        item2_2.put("subValue", getRandomNumber(1, 100));
        items2.add(item2_1);
        items2.add(item2_2);
        secondItem.put("items", items2);

        dataList.add(firstItem);
        dataList.add(secondItem);

        result.put("dataList", dataList);
        return getOkResponseResult(result);
    }
}
